package com.westa.smallshop.data.source.remote;

import android.content.Context;
import android.support.annotation.Nullable;

import com.android.volley.Response;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.data.HttpError;
import com.westa.smallshop.data.source.UserDataSource;
import com.westa.smallshop.util.HttpUtils;
import com.westa.smallshop.util.UserSPUtils;
import com.westa.smallshop.util.volley.FastJsonArrayRequest;
import com.westa.smallshop.util.volley.PageRequest;
import com.westa.smallshop.util.volley.VolleySingleton;

import java.util.List;

import timber.log.Timber;

/**
 * Created by cy on 11/5/16.
 */

public class UserRemoteDataSource implements UserDataSource {
    private static final String USER_TAG = "user_tag";
    private static final String REQUEST_GOODS_LIST_URL = HttpUtils.getHost() + "myself";

    private Context mContext;

    public UserRemoteDataSource(Context context) {
        mContext = context;
    }

    @Override
    public void getGoodsList(final LoadListCallBack<GoodsItem> loadListCallBack) {

        FastJsonArrayRequest<GoodsItem> request = new FastJsonArrayRequest<>(REQUEST_GOODS_LIST_URL,
                GoodsItem.class,
                new Response.Listener<List<GoodsItem>>() {
                    @Override
                    public void onResponse(List<GoodsItem> response) {
                        loadListCallBack.onRefreshDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadListCallBack.onLoadDataFailed();
                    }
                });

        request.addParam("account", UserSPUtils.getAccount(mContext));
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, USER_TAG);
    }

    @Override
    public void cancelLoadRequest() {
        VolleySingleton.cancelRequestForStartWithTag(USER_TAG);
    }
}
